/*
 * Copyright 2010 Pierre-Yves Ricau
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.excilys.ptitwrap.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;

import com.excilys.ptitwrap.NewWrapEater;
import com.excilys.ptitwrap.R;
import com.excilys.ptitwrap.ViewHelper;
import com.excilys.ptitwrap.WrapResource;
import com.excilys.ptitwrap.WrapResourceBuilder;

/**
 * @author Pierre-Yves Ricau
 * 
 */
public class AddWrapEater extends Activity {

	private static final String	TAG	= AddWrapEater.class.getSimpleName();

	private WrapResource		wrapResource;

	private EditText			usernameInput;
	private EditText			wrapNumberInput;

	/** Called when the activity is first created. */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		wrapResource = WrapResourceBuilder.build(this);

		setContentView(R.layout.add_wrap_eater);

		findViews();

		addListeners();

	}

	/**
	 * Adding listeners to view components
	 */
	private void addListeners() {
		findViewById(R.id.add_wrap_eater) //
				.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						addWrapEater();
					}
				});
	}

	/**
	 * Shows a progress dialog while adding the eater to the server. Will call
	 * {@link #addWrapDone()} or {@link #addWrapFailed(RuntimeException)}
	 * depending on the result.
	 */
	private void addWrapEater() {
		final int wrapNumber = ViewHelper.inputAsInt(wrapNumberInput);

		if (wrapNumber == 0) {
			Toast.makeText(this, R.string.zero_wraps, Toast.LENGTH_LONG).show();
			return;
		}

		final String username = usernameInput.getText().toString().trim();

		if (username.equals("")) {
			Toast.makeText(this, R.string.no_username, Toast.LENGTH_LONG).show();
			return;
		}

		final ProgressDialog dialog = new ProgressDialog(this);
		dialog.setCancelable(false);
		dialog.setTitle(R.string.loading_add_title);
		dialog.setMessage(getString(R.string.loading_add_message));
		dialog.show();

		new AsyncTask<Void, Void, RuntimeException>() {
			@Override
			protected RuntimeException doInBackground(Void... params) {
				try {
					wrapResource.addWrapEater(new NewWrapEater(username, wrapNumber));
					return null;
				} catch (RuntimeException e) {
					Log.e(TAG, "An exception occured while adding a wrap eater", e);
					return e;
				}
			}

			@Override
			protected void onPostExecute(RuntimeException exception) {
				dialog.cancel();
				if (exception == null) {
					addWrapDone();
				} else {
					addWrapFailed(exception);
				}

			};

		}.execute();

	}

	/**
	 * Going back to previous activity, with a small message to the user, when
	 * adding is done
	 */
	private void addWrapDone() {
		Toast.makeText(this, R.string.add_eater_done, Toast.LENGTH_LONG).show();
		finish();
	}

	/**
	 * Showing error message when adding an eater failed
	 * 
	 * @param exception
	 */
	private void addWrapFailed(RuntimeException exception) {
		Toast.makeText(this, String.format(getString(R.string.loading_add_failed), exception.getMessage()), Toast.LENGTH_LONG).show();
	}

	/**
	 * Retrieve view components from the inflated layout
	 */
	private void findViews() {
		usernameInput = (EditText) findViewById(R.id.username);
		wrapNumberInput = (EditText) findViewById(R.id.wrap_number);
	}

}
